package cn.yllr.tomgxianghui.service.impl;

import cn.yllr.tomgxianghui.dao.OrderMapper;
import cn.yllr.tomgxianghui.domain.Order;
import cn.yllr.tomgxianghui.entity.User;
import cn.yllr.tomgxianghui.model.PartOrderDetail;
import cn.yllr.tomgxianghui.service.OrderService;
import cn.yllr.tomgxianghui.util.RedisUtil;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Service
public class OrderServiceImpl implements OrderService{

    @Resource
    private OrderMapper mapper;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 生成订单和订单详情表
     * @param cid
     * @param address
     * @param price
     * @return
     */

    @Override
    public Boolean addorder(String cid, String address,Integer price,String put) {
        //生成订单信息
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        //生成订单随机数
        String oid="";
        Random random=new Random();
        for (int i = 0; i < 10; i++) {
            oid += String.valueOf(random.nextInt(10));
        }
        System.out.println("oid = " + oid);
        Boolean flag = mapper.addorders(cid, oid, date, 0, address, price,1);
        //生成订单详情
        Gson gson = new Gson();
        PartOrderDetail[] partOrderDetails = gson.fromJson(put, PartOrderDetail[].class);
        List<PartOrderDetail> partOrderDetails1 = Arrays.asList(partOrderDetails);
        for (PartOrderDetail partOrderDetail : partOrderDetails1) {
            partOrderDetail.setOrderid(oid);
        }
        int i = mapper.insertOrdlist(partOrderDetails1);
        System.out.println("i = " + i);
        redisUtil.set(oid,cid,3600);                  //开始计时，10s后订单过期
        return flag;
    }

    @Override
    public List<Order> findorder() {
        return mapper.findorders();
    }

    @Override
    public Order findOrderByCid(String cid) {

        return mapper.findorderbycid(cid);
    }

    @Override
    public int deleteOrders(String oid) {
         return mapper.deleteorders(oid);
    }

    /**
     * 展示订单内的详细的商品信息
     * @param oid
     * @return
     */
    @Override
    public List<PartOrderDetail> findorderDetail(String oid) {
        return mapper.findDetail_order(oid);
    }

    /**
     * 根据用户名查找电话号码
     * @param name
     * @return
     */
    @Override
    public User findUserPhoneBysession(String name) {
        return mapper.findphoneByid(name);
    }

    @Override
    public int cancelOrderByid(String orderid) {

        return mapper.cancelOrderbyid(orderid);
    }

    @Override
    public List<Order> findOrderByCookid(String customerid) {
        return mapper.findOrderbycookid(customerid);
    }

    /**
     * 同意预约后更新订单状态位为1，同意
     * @param orderid
     * @return
     */
    @Override
    public int agreeAppUpdateOrder(String orderid) {
        return mapper.agreeupdateOrder(orderid);
    }

    @Override
    public int refuseAppUpdateOrder(String orderid) {
        return mapper.refuseupdateOrder(orderid);
    }

    /**
     * 更新订单支付状态
     * @param orderid
     * @return
     */
    @Override
    public int updatePayStatus(String orderid) {
        return mapper.updatestatusForpay(orderid);
    }

}
